#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
  int pipefd[2] = {0};
  if(pipe(pipefd) != 0)
  {
    perror("pipe error!");
    return 1;
  }

  if(fork() > 0)
  {

    //parent
    close(pipefd[0]);
    const char*msg = "i am father\n";
    while(1)
    {
      write(pipefd[1], msg, strlen(msg));
      sleep(1);
    }
    exit(0);
  }
  //child
  else 
  {
  close(pipefd[1]);
  while(1)
  {
    char buffer[64] = {0};
    ssize_t s = read(pipefd[0], buffer, sizeof(buffer)-1);
    if(s == 0)
    {
      break;
    }
    else if(s > 0)
    {
      buffer[s] = 0;
      printf("child say# %s\n", buffer);
    }
    else {
      break;
    }
  }
  }

  return 0;
}
